// pages/orderDetail/orderDetail.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_id: '',
    info: '',
    show: false,
    images: [],
    safeBottom: `30px`,
    settle: ''
  },
  
  copy(event){
    wx.setClipboardData({
      data: event.currentTarget.dataset.content,
      success (res) {
        util.toast('已复制')
      }
    })
  },
  navigation(){
    wx.openLocation({
      latitude : Number(this.data.info.orderAddress.lat),
      longitude : Number(this.data.info.orderAddress.lng),
      name: this.data.info.orderAddress.area,
      scale: 18
    })
  },
  call(){
    wx.makePhoneCall({
      phoneNumber: this.data.info.orderAddress.mobile
    })
  },
  onClose(){
    this.setData({
      show: false
    })
  },
  showUpload(){
    this.setData({
      show: true
    })
  },
  delImages(e){
    let index = e.currentTarget.dataset.index
    this.data.images.splice(index,1)
    this.setData({
      images: this.data.images
    })
  },
  chooseImages(e) {
    let index = e.currentTarget.dataset.index
    http.chooseImg(['camera'], true).then(res => {
      let arr = this.data.images;
      if(index !== undefined){
        arr.splice(index,1,res.data.url)
      }else {
        arr.push(res.data.url)
      }
      this.setData({
        images: arr
      })
    })
  },
  getInfo(){
    http.get('order/orderInfo', {
      id: this.data.order_id
    }).then(res => {
      this.setData({
        info: res.data
      })
      if(res.data.is_settle > 0 ){
        this.getSettle()
      }
      
    })
  },
  getSettle(){
    http.get('order/priceInfo', {
      id: this.data.order_id
    }).then(res => {
      this.setData({
        settle: res.data
      })
    })
  },
  accept(){
    http.get('order/skillaccept',{
      id:  this.data.order_id
    }, true, false).then(res => {
      util.toast('抢单成功')
      this.getInfo()
    }).catch(e => {
      let data = e.data
      if(data.code === 2){
        util.toast(data.msg)
      }else if(data.code === 0){
        util.toast(data.data)
      }
    })
  },
  go(){
    http.get('order/skillgo', {
      id: this.data.order_id
    }).then(res => {
      this.getInfo()
    })
  },
  arrive(){
    if(this.data.images.length === 0) return util.toast('请上传图片')
    http.get('order/skillreach', {
      id: this.data.order_id,
      reach_images: this.data.images.join(',')
    }).then(res => {
      this.setData({
        images: [],
        show: false
      })
      this.getInfo()
    })
  },
  finish(){
    if(this.data.images.length === 0) return util.toast('请上传图片')
    http.get('order/skillfinish', {
      id: this.data.order_id,
      finish_images: this.data.images.join(',')
    }).then(res => {
      this.setData({
        images: [],
        show: false
      })
      this.getInfo()
    })
  },
  start(){
    http.get('order/skillstart', {
      id: this.data.order_id
    }).then(res => {
      this.getInfo()
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const high = app.globalData.safeBottom
    this.setData({
      order_id: options.id,
      safeBottom: `${high}px`
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})